<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.traineeName" placeholder="受训人" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.deptName" placeholder="部门" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.type" placeholder="培训类型">
        <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-input v-model="listQuery.trainerName" placeholder="培训人" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.lsdate" placeholder="最后修改时间" style="width: 200px;" class="filter-item" type="date" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.ledate" placeholder="最后修改时间" style="width: 200px;" class="filter-item" type="date" @keyup.enter.native="handleFilter" />

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        导出
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-clean" @click="handleReset">
        重置
      </el-button>
    </div>
    <el-table ref="dragTable" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="90"
      >
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span></template>
      </el-table-column>
      <el-table-column align="center" label="姓名" width="100">
        <template slot-scope="scope">
          <span style="font-weight: 700; text-decoration: underline;" @click="gotoDetailAll(scope.row)">{{ scope.row.trainee.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="工号" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.trainee.empNo }}</span></template>
      </el-table-column>
      <el-table-column class-name="status-col" label="部门" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.deptid.deptName }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="培训类型">
        <template slot-scope="scope">
          <span v-if="scope.row.typeFlag==1" style="color:red;">新员工培训</span>
          <span v-if="scope.row.typeFlag==2" style="color:green;">年度再培训</span>
          <span v-if="scope.row.typeFlag==3" style="color:blue;">轮岗培训</span>
        </template>
      </el-table-column>
      <el-table-column min-width="240px" label="主题">
        <template slot-scope="scope">
          <span>{{ scope.row.subject }}</span></template>
      </el-table-column>

      <el-table-column width="100px" label="培训开始时间">
        <template slot-scope="scope">
          <span>{{ scope.row.sdate==null? ' ':scope.row.sdate| parseTime('{yy}-{mm}-{dd} ') }}</span></template>
      </el-table-column>

      <el-table-column width="100px" label="培训结束时间">
        <template slot-scope="scope">
          <span>{{ scope.row.edate==null? ' ':scope.row.edate| parseTime('{y}-{m}-{d} ') }}</span></template>
      </el-table-column>
      <el-table-column align="center" label="培训人" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.trainer.userName }}</span></template>
      </el-table-column>
      <el-table-column align="center" label="是否合格" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.grade>=60? '合格' :'不合格' }}</span></template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageQ" :limit.sync="listQuery.limit" @pagination="fechData" />
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="ID" prop="id">
          <el-input v-model="temp.id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getlistAll } from '@/api/trainNew'
import Sortable from 'sortablejs'
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
export default {
  name: 'TrainNew',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      editList: null,
      trainer: null,
      trainee: null,
      sortable: null,
      oldList: [],
      newList: [],
      // 参数条件
      listQuery: {
        pageQ: 1,
        limit: 20,
        traineeName: null,
        deptName: null,
        type: null,
        trainerName: null,
        sdate: null,
        ledate: null
      },
      typeOptions: [{ value: 1, label: '新员工培训' }, { value: 2, label: '年度再培训' }, { value: 3, label: '轮岗培训' }],
      temp: {
        id: undefined,
        timestamp: new Date()
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        detail: '明细'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.fechData()
  },
  methods: {
    gotoDetailAll(row) {
      // 指定跳转地址
      const row1 = JSON.parse(JSON.stringify(row))
      this.temp = Object.assign({}, row1)
      this.userId = this.temp.trainee.userId
      this.userInfo = this.temp.trainee.userName + ' - ' + this.temp.trainee.empNo + ' - ' + this.temp.deptid.deptName
      this.$router.replace('/train/detailAll?userId=' + this.userId + '&userInfo=' + this.userInfo)
    },
    fechData() {
      getlistAll(this.listQuery).then(response => {
        this.listLoading = true
        this.list = response.ListAll
        this.showList = response.ListAll
        this.listLoading = false
        this.total = response.count
      })
    },
    handleDownload() {

    },
    handleFilter() {
      this.listQuery.pageQ = 1
      this.fechData()
    },
    handleReset() {
      this.listQuery = {
        traineeName: null,
        deptName: null,
        type: null,
        trainerName: null,
        lsdate: null,
        ledate: null
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    setSort() {
      const el = this.list[0]

      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost',
        setData: function(dataTransfer) {
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)

          const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
          this.newList.splice(evt.newIndex, 0, tempIndex)
        }
      })
    }
  }
}
</script>

<style>
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #42b983!important;
}
</style>

<style scoped>
.icon-star{
  margin-right:2px;
}
.drag-handler{
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d{
  margin-top: 15px;
}
</style>

